package com.yulebaby.callsenter.ui.activity;


import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.bean.CustomerBean;
import com.yulebaby.callsenter.bean.ResultBean;
import com.yulebaby.callsenter.database.manager.Dao;
import com.yulebaby.callsenter.event.EventFactory;
import com.yulebaby.callsenter.event.RefreshEvent;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.task.UploadCustomerTask;
import com.yulebaby.callsenter.ui.base.BaseActivity;
import com.yulebaby.callsenter.ui.view.ToSeaSoonSaveDialog;
import com.yulebaby.callsenter.utils.DialogUtils;
import com.yulebaby.callsenter.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 保存售前结果界面
 */
public class SavePreSaleResultActivity extends BaseActivity {


    @BindView(R.id.tb_title)
    Toolbar mTbTitle;
    @BindView(R.id.iv_result)
    ImageView mIvResult;
    @BindView(R.id.tv_result)
    TextView mTvResult;
    @BindView(R.id.btn_lookcustomer)
    Button mBtnLookcustomer;
    @BindView(R.id.btn_backmycustomer)
    Button mBtnBackmycustomer;
    @BindView(R.id.btn_throwsea)
    Button mBtnThrowsea;
    private CustomerBean customerbean;
    private Long customer_id;
    private ToSeaSoonSaveDialog toSeaSoonSaveDialog;
    private ProgressDialog progressDialog;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_save_pre_sale_result;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().post(EventFactory.CreateRefreshEvent(RefreshEvent.refreshDataFromInternet));
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        customerbean = (CustomerBean) intent.getSerializableExtra(Constant.CUSTOMER_BEAN);
        customer_id = customerbean.getCustomer_id();
    }


    @OnClick({R.id.btn_lookcustomer, R.id.btn_backmycustomer, R.id.btn_throwsea})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_lookcustomer:
                Intent preintent = new Intent(this, PreSaleRemarkAcitivity.class);
                preintent.putExtra(Constant.CUSTOMER_BEAN, customerbean);
                startActivity(preintent);
                finish();
                break;
            case R.id.btn_backmycustomer:
                startActivity(new Intent(SavePreSaleResultActivity.this, MainActivity.class));
                finish();
                break;
            case R.id.btn_throwsea:
                showToSeaSoonDialog();
                break;
        }
    }

    /**
     * 投公海
     */
    private void showToSeaSoonDialog() {
        toSeaSoonSaveDialog = new ToSeaSoonSaveDialog(this, true, new ToSeaSoonSaveDialog.DialogCallBack() {
            @Override
            public void onConfirm(Dialog dialog) {
                progressDialog = DialogUtils.showProgressDlg(SavePreSaleResultActivity.this, null);
                UploadCustomerTask.doThrowCustomerInternationalWater(SavePreSaleResultActivity.this, customer_id, new HttpCallback<ResultBean>() {
                    @Override
                    public void onSuccess(ResultBean result) {
                        super.onSuccess(result);
                        progressDialog.dismiss();
                        toSeaSoonSaveDialog.dismiss();
                        ToastUtil.showToast("投公海成功");
                        Dao.getInstance().deleteBean(customerbean);
                        EventBus.getDefault().post(EventFactory.CreateRefreshEvent(RefreshEvent.refreshDataFromInternet));
                        EventBus.getDefault().post(EventFactory.CreateQuitEvent());
                        startActivity(new Intent(SavePreSaleResultActivity.this, MainActivity.class));
                        finish();
                    }
                });
            }

            @Override
            public void onCancel(Dialog dialog) {
                toSeaSoonSaveDialog.dismiss();
            }
        });
        toSeaSoonSaveDialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().post(EventFactory.CreateRefreshEvent(RefreshEvent.refreshDataFromDatabase));
    }
}




